package com.yichi.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yichi.R;
import com.yichi.YiChiApplication;
import com.yichi.model.BatteryModel;

import java.util.List;

public class OperateAdapter extends RecyclerView.Adapter<OperateAdapter.BatteryStatuswHolder> {
    private List<BatteryModel> batteryModels;
    private int currentCheckedPosition = -1;
    private ItemClickCallback itemClickCallback;

    public OperateAdapter(List<BatteryModel> batteryModels) {
        this.batteryModels = batteryModels;
    }

    public ItemClickCallback getItemClickCallback() {
        return itemClickCallback;
    }

    public void setItemClickCallback(ItemClickCallback itemClickCallback) {
        this.itemClickCallback = itemClickCallback;
    }

    // 更新某个 item
    public void updateItem(int position, BatteryModel batteryModel) {
        batteryModels.set(position, batteryModel);
        notifyItemChanged(position);  // 通知 RecyclerView 刷新该项
    }

    @NonNull
    @Override
    public BatteryStatuswHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_battery_status, parent, false);
        return new BatteryStatuswHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BatteryStatuswHolder holder, int position) {
        final int position1 = position;
        BatteryModel batteryModel = batteryModels.get(position);
        switch (batteryModel.getBatteryLogo()) {
            case 1:
                holder.battery_picture.setVisibility(View.VISIBLE);
                holder.battery_picture.setImageResource(R.mipmap.batary1);
                break;
            case 2:
                holder.battery_picture.setVisibility(View.VISIBLE);
                holder.battery_picture.setImageResource(R.mipmap.batary2);
                break;
            case 3:
                holder.battery_picture.setVisibility(View.VISIBLE);
                holder.battery_picture.setImageResource(R.mipmap.batary3);
                break;
            case 4:
                holder.battery_picture.setVisibility(View.GONE);
                break;
        }

        if (batteryModel.getBatteryTip().equals("")) {
            holder.battery_tip.setVisibility(View.GONE);
        } else {
            holder.battery_tip.setVisibility(View.VISIBLE);
            holder.battery_tip.setText(batteryModel.getBatteryTip());
            if (batteryModel.isFull()) {
                holder.battery_tip.setBackgroundResource(R.drawable.battery_tip_bg);
            } else {
                holder.battery_tip.setBackgroundResource(R.drawable.battery_tip_bg2);
            }
        }

        holder.operate_tip.setText(batteryModel.getOperateTip());
        if (batteryModel.getOperateTip().equals("空") || batteryModel.getOperateTip().equals("不可存")) {
            holder.operate_tip.setTextColor(YiChiApplication.application.getResources().getColor(R.color.can_not_use_text_color));
            holder.battery_picture.setAlpha(0.4f);
            holder.itemView.setClickable(false);
            holder.itemView.setEnabled(false);
        } else {
            holder.operate_tip.setTextColor(YiChiApplication.application.getResources().getColor(R.color.can_use_text_color));
            holder.battery_picture.setAlpha(1f);
            holder.itemView.setClickable(true);
            holder.itemView.setEnabled(true);
        }

        if(batteryModel.isChecked()){
            holder.itemView.setBackgroundResource(R.drawable.battery_status_checked_bg);
        }else{
            holder.itemView.setBackgroundResource(R.drawable.battery_status_bg);
        }

        holder.battery_position.setText(batteryModel.getPosition());
        if (batteryModel.isClick()) {
            holder.itemView.setOnClickListener(null);
            holder.itemView.setOnClickListener(view -> {
                if(currentCheckedPosition != -1){
                    batteryModels.get(currentCheckedPosition).setChecked(false);
                }
                currentCheckedPosition = position1;
                batteryModel.setChecked(true);
                if (itemClickCallback != null) {
                    itemClickCallback.handleItemClick(batteryModel);
                }
                notifyDataSetChanged();
            });
        } else {
            holder.itemView.setOnClickListener(null);
        }
    }

    @Override
    public int getItemCount() {
        return batteryModels.size();
    }

    static class BatteryStatuswHolder extends RecyclerView.ViewHolder {
        ImageView battery_picture;
        TextView battery_position, battery_tip, operate_tip;

        BatteryStatuswHolder(View itemView) {
            super(itemView);
            battery_picture = itemView.findViewById(R.id.battery_picture);
            battery_position = itemView.findViewById(R.id.battery_position);
            battery_tip = itemView.findViewById(R.id.battery_tip);
            operate_tip = itemView.findViewById(R.id.operate_tip);
        }
    }

    public interface ItemClickCallback {
        void handleItemClick(BatteryModel batteryModel);
    }

}
